package com.logon.main;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Main implements Runnable, ActionListener {

	public static String usernameText = "";
	public static String passwordText = "";
	
	public static JFrame logonWindow = new JFrame("Login");
	public static JTextField username = new JTextField("Username");
	public static JPasswordField password = new JPasswordField("Password");
	public static JButton submit = new JButton("Login");
	
	public static JFrame scriptWindow = new JFrame("Choose Option");
	public static JRadioButton Python = new JRadioButton("Python Script");
	public static JButton enter = new JButton("Select");
	
	JTextField scriptName = new JTextField("Enter Script Name Here");
	public String pythonScriptName;
	
	public static void main(String[] args) {
		new Main().start();

	}

	@SuppressWarnings("deprecation")
	public void openFile(int lineWanted) {
		lineWanted =+ 1;
		if (username.getText().equals("Username")) {
			JOptionPane.showMessageDialog(null, "Please enter a valid username.", "Error", JOptionPane.ERROR_MESSAGE);
		} else {
			FileInputStream fs;
			try {
				fs = new FileInputStream("C:/Program Files (x86)/Oliver Keay/Login Application/Login Application/com/accounts/"+username.getText()+".txt");
				@SuppressWarnings("resource")
				BufferedReader br = new BufferedReader(new InputStreamReader(fs));
				for(int i = 1; i < lineWanted; i++)
				  br.readLine(); 
				String line = br.readLine();
				if (lineWanted == 1 && password.getText().equals(line)) {
					/*
					 * Add here the function here that you want to run now that the user has logged in.
					 */
					JOptionPane.showMessageDialog(null, "Welcome "+username.getText(), "Welcome", JOptionPane.INFORMATION_MESSAGE);
					runScript();
				} else {
					JOptionPane.showMessageDialog(null, "The password you have entered is incorrect.", "Error", JOptionPane.ERROR_MESSAGE);
					System.out.println(line);
					System.out.println(password.getText());
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	@SuppressWarnings("unused")
	private synchronized void start() {
		boolean running = true;
		new Thread(this).start();
	}
	
	@SuppressWarnings("unused")
	private synchronized void stop() {
		boolean running = false;
		
	}
	
	public void runScript() {
		logonWindow.dispose();
		JOptionPane.showMessageDialog(null, "Please place scripts in  C:/Program Files (x86)/Oliver Keay/Login Application/Login Application/scripts/", "Info", JOptionPane.INFORMATION_MESSAGE);
		scriptWindow.setVisible(true);
		scriptWindow.setSize(450, 300);
		scriptWindow.add(Python, BorderLayout.NORTH);
		scriptWindow.add(scriptName, BorderLayout.CENTER);
		scriptWindow.add(enter, BorderLayout.SOUTH);
		enter.addActionListener(this);
		enter.setActionCommand("script");
	}
	
	@Override
	public void run() {
		JOptionPane.showMessageDialog(null, "To make an account, create a text file with the name of the account as the name of the file, and on the first line place your password in  C:/Program Files (x86)/Oliver Keay/Login Application/Login Application/com/accounts/", "Info", JOptionPane.INFORMATION_MESSAGE);
		logonWindow.setVisible(true);
		logonWindow.setLocationRelativeTo(null);
		logonWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		logonWindow.setSize(300, 100);
		logonWindow.add(submit, BorderLayout.SOUTH);
		logonWindow.add(username, BorderLayout.NORTH);
		logonWindow.add(password, BorderLayout.CENTER);
		submit.setSize(200, 100);
		submit.addActionListener(this);
		submit.setActionCommand("submit");
	}
	
	
	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("submit")) { 
			usernameText = username.getText();
			passwordText = password.getText();
			openFile(1);
		} else if (e.getActionCommand().equals("script")) {
			if (Python.isSelected()) {
				Runtime rt = Runtime.getRuntime();
			    try {
			        rt.exec(new String[]{"cmd.exe","/c","start python C:/Program Files (x86)/Oliver Keay/Login Application/Login Application/scripts"+scriptName.getText()+".py"});
			    } catch (IOException e1) {
			        // TODO Auto-generated catch block
			        e1.printStackTrace();
			    }
			}
		}
	}
}
